#!/usr/bin/env python
#coding=utf-8
from BeautifulSoup import BeautifulSoup 
import urllib,time, datetime
from pysqlite2 import dbapi2 as sqlite

import utils,code
from ex import NoDataToday 

#url = "http://biz.finance.sina.com.cn/company/history.php?symbol=sh%s"
url= "http://stock.business.sohu.com/q/hp.php?code=%s"
class Log4today:
    def __init__(self, fn="/home/jay/today.log"):
        self.d = {}
        self.fn = fn
        
    def add(self, date, code):
        if not self.d.has_key(date): 
            self.d[date] = []
        self.d[date].append(code)
        
    def close(self):
        f = open(self.fn , 'a')
        for k in self.d.keys():
            f.write(k)
            f.write('\n')
            for c in self.d[k]:
                f.write('\t%s\n'%c)
            f.write('\n')
        f.close()
        
log = Log4today()
        
def get_1day_data(d, c):
    items = ['open', 'high','low','close', 'volume']
    data = []
    result = {}
    
    u = url%c
    s = utils.get_soup(u)
    
    t = s.find(text=d)
    
    if not t:
        log.add(d, c) 
        raise NoDataToday(d, c)
    
    row = t.parent.parent
    for td in row.contents[2:7]:
        data.append(td.string)
        
    for k,v in zip(items, data):
        result[k] = v
        
    close = row.contents[5].contents[0].string
    result['close'] = close
           
    result = {d: result}
    return result

scoder = code.get_coder()

con = sqlite.connect('/home/jay/stock.db')
cursor = con.cursor()
insert_sql = "insert into prices(date,code, open, close, high, low, volume) values(?,?,?,?,?,?,?)"

#date = datetime.date(2007, 6, 29).isoformat()
#date = utils.get_today_isostr()
date = utils.get_sohu_datestr(2007, 6, 29)
i = 0

while True:
    try:
        code = unicode(scoder.next()[0])
        #if code == '600003':break
    except KeyError:
        break
    
    try:
        r = get_1day_data(date, code)
    except NoDataToday, e:
        continue
        print e
    else:
        data = r.values()[0]
        
        v = [date, code, data['open'], data['close'], data['high'],data['low'], data['volume']]
        print v
        cursor.execute(insert_sql, v)
        
        i = i + 1
        if i%10 == 0:
            con.commit() 
            print i, 'commited' 
                  
con.commit()
cursor.close()
con.close()
log.close()